// pages/shou/shou.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    heights: "",
    title: '',
    source: 'EabEnd', //EabEnd 企商  Consumer 消费  Trative  行政
  },
  // 授权
  getUserProfile() {
    // 登录
    wx.login({
      success: res => {
        this.setData({
          code: res.code
        })
      }
    })
    wx.getUserProfile({
      desc: '获取用户信息',
      success: (res) => {
        this.setData({
          encryptedData: res.encryptedData,
          iv: res.iv,
        })
        wx.showLoading({
          title: '加载中',
          icon: 'none'
        })
        var that = this
        this.myRequest(yuming + 'Besall/doLogin', {
          shopId: wx.getStorageSync('shopId'),
          code: that.data.code,
          encryptedData: that.data.encryptedData,
          iv: that.data.iv,
          source: that.data.source
        }, 'POST').then(res => {
          if (res.data.code == 200) {
            wx.hideLoading({})
            wx.setStorageSync('token', res.data.data.Token)
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
            if (that.data.sta == 66) {
              wx.navigateBack({
                delta: 0,
              })
            } else if (that.data.sta != 0) {
              setTimeout(function () {
                wx.reLaunch({
                  url: '/page_zhu/login/login',
                })
              }, 1000)
            } else {
              if (that.data.url != "pages/index/index") {
                console.log(1)
                wx.navigateTo({
                  url: "/" + that.data.url + "?mobile=" + that.data.mobile,
                })
              } else {
                console.log(2)
                wx.switchTab({
                  url: '/' + that.data.url,
                })
              }
            }
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      sta: options.sta,
      url: options.url,
      mobile: options.mobile
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})